package com.slz3.business.module.memberManagement.userScoreRecord.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.userScoreRecord.domain.UserScoreRecord;
import com.slz3.business.module.memberManagement.userScoreRecord.domain.UserScoreRecordFactory;
import com.slz3.business.module.memberManagement.userScoreRecord.steward.UserScoreRecordSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class UserScoreRecordServiceImpl implements UserScoreRecordService {
    @Override
    public UserScoreRecord save(UserScoreRecord userScoreRecord) {
        return Repositorys.userScoreRecord.save(UserScoreRecordFactory.initBaseFields(userScoreRecord));
    }
    @Override
    public Page<UserScoreRecord> page(MultiValueMap<String, String> params) {
        Page<UserScoreRecord> userScoreRecordPage = Repositorys.userScoreRecord.findAll(
                UserScoreRecordSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return userScoreRecordPage;
    }


    @Override
    public UserScoreRecord modify(UserScoreRecord userScoreRecordNew) {
        return Repositorys.userScoreRecord.saveAndFlush(UserScoreRecordFactory.initBaseFields(userScoreRecordNew));
    }

    @Override
    public Optional<UserScoreRecord> single(UUID uuid) {
        return Repositorys.userScoreRecord.findById(uuid);
    }

    @Override
    public List<UserScoreRecord> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.userScoreRecord.findAllById(uuids);
    }

    @Override
    public List<UserScoreRecord> delete(List<UserScoreRecord> userScoreRecordList) {
        userScoreRecordList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            UserScoreRecordFactory.initBaseFields(item);
        });
        return Services.userScoreRecordTransactional.batchModify(userScoreRecordList);
    }

}
